<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";

import { useUserStore } from "@/stores/user";

definePage({
	name: "LoginPage",
	meta: { title: "登录", asside: false },
});
defineOptions({ name: "IndexPage" });

const title = import.meta.env.VITE_APP_TITLE;

const form = reactive({
	principal: "",
	password: "",
});
const rules = ref({
	principal: [{ required: true, message: "请输入账号" }],
	password: [{ required: true, message: "请输入密码" }],
});

const formRef = shallowRef<FormInstance>();

const router = useRouter();

const userStore = useUserStore();

const { execute, isLoading } = useAsyncState(
	async () => {
		await formRef.value!.validate();
		await userStore.login(form.principal, form.password);
		ElMessage.success("登录成功");
	},
	null,
	{
		immediate: false,
	},
);

watchEffect(() => {
	if (userStore.isLogined) {
		router.replace({ path: "/" });
	}
});
</script>

<template>
	<div class="login-page">
		<div class="login-page-container" relative>
			<div><span class="left-title">安全生产培训系统</span></div>
			<div class="login-container">
				<div class="app-title">
					{{ title }}
				</div>
				<div class="sub-title">用户登录</div>
				<el-form
					ref="formRef"
					:rules="rules"
					:model="form"
					:disabled="isLoading"
				>
					<el-form-item>
						<el-input
							v-model="form.principal"
							placeholder="请输入账号"
							clearable="true"
						>
							<template #prefix>
								<el-icon class="i-custom:username" />
							</template>
						</el-input>
					</el-form-item>
					<el-form-item>
						<el-input
							v-model="form.password"
							placeholder="请输入密码"
							type="password"
							show-password
							:clearable="true"
						>
							<template #prefix>
								<el-icon class="i-custom:password" />
							</template>
						</el-input>
					</el-form-item>
				</el-form>
				<el-button
					class="login-button"
					:loading="isLoading"
					@click="() => execute()"
					type="primary"
				>
					登录
				</el-button>
			</div>
		</div>
	</div>
</template>

<style scoped lang="scss">
.login-page {
	display: flex;
	justify-content: center;
	align-items: center;
	width: 100vw;
	height: 100vh;
	background-image: url("./assets/bg.png");
	background-size: 100% 50%;
	background-repeat: no-repeat;
	background-position: 0% 0%;
	.el.message {
		background-color: #ffffff;
	}
}

.login-page-container {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	width: 85vw;
	height: 75vh;
	position: relative;
	margin-top: 0px;
	.left-title {
		font-family:
			PingFangSC,
			PingFang SC;

		font-size: 36px;
		letter-spacing: 2px;
		color: #f8f8f8;
		line-height: 62px;
		text-align: left;
		font-style: normal;
	}
}

.login-container {
	display: flex;
	flex-direction: column;
	width: 450px;
	height: 450px;
	margin-top: 20px;
	padding: 10px 44px;
	background: #ffffff;
	box-shadow: 10px 4px 20px 5px rgba(158, 158, 158, 0.3);
}

.app-title {
	font-size: 34px;
	font-weight: bold;
	line-height: 48px;
	color: #eaf7ff;
	text-align: center;
}

.sub-title {
	position: relative;
	margin: 10px 0 24px;
	font-family: AppleSystemUIFont;
	font-size: 19px;
	font-weight: 600;
	color: #2d2f3e;
	line-height: 56px;
	text-align: left;
	font-style: normal;

	&::before {
		left: -50px;
	}

	&::after {
		right: -50px;
	}
}

.login-button {
	height: 45px;
	margin: 14px 0 0px;
	margin-left: 0;
	font-size: 16px;
	font-weight: 500;
	background: #165dff;
	box-shadow: inset 0 0 5px 0 rgb(247 251 255 / 50%);
	border-radius: 5px;
	--el-color-primary: #fff;
}

:deep(.el-input__wrapper) {
	height: 45px;
	background-color: #fff;
	color: #9597a2;
}
:deep(.el-form-item) {
	margin-top: 17px;
}
:deep(.el-icon) {
	font-size: 18px !important;
	color: 6F6F8A !important;
}
:deep(.el-input__inner) {
	font-size: 14px;
}
:deep(.el-input) {
}
</style>
